package com.cskaoyan.controller.promotion;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.CommonData;
import com.cskaoyan.bean.bo.promotionbo.AdminBo;
import com.cskaoyan.bean.bo.promotionbo.MarketTopicBo;
import com.cskaoyan.bean.po.promotionpo.MarketTopic;
import com.cskaoyan.bean.vo.promotionvo.*;
import com.cskaoyan.exception.MarketValidationException;
import com.cskaoyan.service.promotion.AdminTopicService;
import com.cskaoyan.util.validation.ValidationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: zhang tao
 * @Description:
 * @Date: 2022/7/15 16:56
 */
@RestController
@RequestMapping("admin/topic")
public class AdminTopicController {

    @Autowired
    AdminTopicService adminTopicService;


    @GetMapping("list")
    public BaseRespVo adminTopicList(AdminBo adminBo) {
        CommonData<MarketTopic> topicList = null;
        try {
            topicList = adminTopicService.getTopics(adminBo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return BaseRespVo.ok(topicList);
    }

    @PostMapping("create")
    public BaseRespVo adminTopicCreate( @Validated @RequestBody MarketTopicBo marketTopicBo,BindingResult result) throws MarketValidationException {
        ValidationUtils.valid(result);
        MarketTopic marketTopic1 = null;
        try {
            marketTopic1 = adminTopicService.insertTopics(marketTopicBo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return BaseRespVo.ok(marketTopic1);
    }

    @PostMapping("update")
    public BaseRespVo adminTopicUpdate(@Validated @RequestBody MarketTopic marketTopic,BindingResult result) throws MarketValidationException {
        ValidationUtils.valid(result);

        MarketTopic marketTopic1=null;
        try {
            marketTopic1 = adminTopicService.updateTopics(marketTopic);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return BaseRespVo.ok(marketTopic1);
    }

    @PostMapping("delete")
    public BaseRespVo adminTopicDelete(@RequestBody MarketTopic marketTopic) {

        try {
            adminTopicService.deleteTopics(marketTopic);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return BaseRespVo.ok(null);
    }

    @PostMapping("batch-delete")
    public BaseRespVo adminTopicDelete(Integer[] ids) {

        try {
            for (Integer id : ids) {
                adminTopicService.deleteBatchTopics(id);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return BaseRespVo.ok(null);
    }

    @GetMapping("read")
    public BaseRespVo readAdminTopic(Integer id) {
        MarketTopicReadVo marketTopicReadVo = null;
        try {
            marketTopicReadVo = adminTopicService.selectTopicAndGoodsById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return BaseRespVo.ok(marketTopicReadVo);
    }

}
